<?php
require "connect_database.php";
require "form.php";
require "calculation.php";

session_start();

    
    
    $sql_get_lastest_loan_serial_num = "SELECT loan_application_serial_no FROM `loan` ORDER BY `loan_id` DESC LIMIT 1 ";
    //$sql_get_lastest_loan_serial_num_query = mysqli_query($connect, $sql_get_lastest_loan_serial_num);
    //$get_loan_no_result = mysql_fetch_object($sql_get_lastest_loan_serial_num_query);
    
    if ($result =  mysqli_query($connect, $sql_get_lastest_loan_serial_num)) {
        while($obj = mysqli_fetch_array($result)){
            $get_loan_no_result = $obj[0];
            
        }
        $get_loan_no_result = $get_loan_no_result + 1;
    }
// temp variable
    $gross_salary = 1000;

    try
    {
        // Initialize form object
        $form = new MyForm($_POST);
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
        exit;
    }

    // Set all local variable for $_POST here
    $hm = $form->getAllKeyValuePairs();
    foreach($hm as $key => $value)
    {
        ${$key} = $value;
    }

    $age = $form->getAge();
    

    try
    {
        // Initialize calculation object
        $calculation = new MyCalculation($age, $martial_status,  $residential_status, $employment_status, $gross_salary_applicant_1, $employment_level);
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
        exit;
    }

    $creditScore = $calculation->creditScore($age, $martial_status,  $residential_status, $employment_status, $gross_salary_applicant_1, $employment_level);
    $_SESSION['creditScore']=$creditScore;
    if($creditScore > 14)
    {
        $loanStatus = "accepted";
    }
    elseif($creditScore < 12)
    {
        $loanStatus = "rejected";
    }
    elseif(($creditScore >11) && ($creditScore < 14)){
        
        $loanStatus = "pending";
    }
    $current_residential_time = ($current_residential_year * 12) + $current_residential_month;
    $previous_residential_time = ($previous_residential_year * 12) + $previous_residential_month;
    $current_employer_time = ($current_employer_year * 12) + $current_employer_month;
    $previous_employer_time = ($previous_employer_year * 12) + $previous_employer_month;

$sql_new_customer = "INSERT INTO `customer` 
(customer_number, customer_title, customer_given_name, customer_surname, customer_gender, customer_type, customer_dob, customer_email, customer_nationality, customer_current_city, customer_visa_type, customer_residential_status, customer_martial_status, customer_home_phone_ext, customer_home_phone_number, customer_work_phone_ext, customer_work_phone_number, customer_fax_ext, customer_fax_number, customer_mobile_phone_ext, customer_mobile_phone_number, customer_occupation, customer_employment_level, customer_employment_status, customer_driver_license_number, customer_driver_license_state, customer_driver_license_expiry_date)
VALUES ('', '$title', '$givenname', '$surname', '$gender', '$applicant_type', '$dob', '$email_address', '$nationality', '$resident_of', '$visa_type', '$residential_status', '$martial_status', '$home_phone_number_ext', '$home_phone_number', '$work_phone_number_ext', '$work_phone_number', '$fax_number_ext', '$fax_number', '$mobile_phone_number_ext', '$mobile_phone_number', '$current_occupation', '$employment_level', '$employment_status', '$licence_number', '$licence_state', '$licence_expiry_date')";

$sql_new_customer_result = mysqli_query($connect, $sql_new_customer);

if(mysqli_affected_rows($connect) <= 0)
{
	//echo "fail";
}
else
{
	//echo "success";

	$customer_id = mysqli_insert_id($connect);
	$twe_current = ($current_employer_year * 12) + $current_employer_month;
	$current_date = date("Y-m-d");

	$sql_new_customer_current_residential = "INSERT INTO `address`(customer_id, address_address, address_postcode, address_twa, address_status) VALUES ('$customer_id', '$current_residential_address', '$current_residential_address_postcode', '$current_residential_time', 'current')";

	$sql_new_customer_postal_residential = "INSERT INTO `address`(customer_id, address_address, address_postcode, address_twa, address_status) VALUES ('$customer_id', '$postal_address', '$postal_address_postcode', '', 'postal')";

	$sql_after_loan_postal_residential = "INSERT INTO `address`(customer_id, address_address, address_postcode, address_twa, address_status) VALUES ('$customer_id', '$after_loan_postal_address', '$after_loan_postal_address_postcode', '', 'after loan')";

	$sql_new_customer_previous_residential = "INSERT INTO `address`(customer_id, address_address, address_postcode, address_twa, address_status) VALUES ('$customer_id', '$previous_residential_address', '$previous_residential_address_postcode', '$previous_residential_time', 'previous')";

	$sql_new_customer_current_employer = "INSERT INTO `employer` (customer_id, employer_name, employer_twe, employer_phone_ext, employer_phone_number, employer_fax_ext, employer_fax_number, employer_address, employer_postcode, employer_occupation, employer_status) VALUES ('$customer_id', '$employer_name', '$twe_current', '$employer_phone_number_ext', '$employer_phone_number', '$employer_fax_number_ext', '$employer_fax_number', '$employer_address', '$employer_address_postcode', '', 'current')";

	$sql_new_address_result = mysqli_query($connect, $sql_new_customer_current_residential);

	if(mysqli_affected_rows($connect) <= 0)
	{
		//echo "fail_address";
	}
	else
	{
		//echo "success_address";
	}
    
	$sql_new_customer_employer_result = mysqli_query($connect, $sql_new_customer_current_employer);
	
	if(mysqli_affected_rows($connect) <= 0)
	{
		//echo "fail_employer";
	}
	else
	{
		//echo "success_employer";
	}

	$sql_new_customer_loan = "INSERT INTO `loan` (customer_id, loan_type,loan_amount, loan_purpose, loan_reason, loan_term, loan_repayment_option, loan_interest_only_year, loan_application_timestamp, loan_status,loan_application_serial_no)
	                           VALUES ('$customer_id', '$loan_type', '$total_loan_amount', '$loan_purpose', '$loan_reason', '$loan_term', '$repayment_options', '$interest_only_period', '$current_date', '$loanStatus','$get_loan_no_result')";
	
	$sql_new_customer_loan_result = mysqli_query($connect, $sql_new_customer_loan);

	if(mysqli_affected_rows($connect) <= 0)
	{
		//echo "fail loan";
	}
	else
	{
		//echo "success loan";
	}
	
	$sql_new_customer_small_assets = "INSERT INTO `small_asset`(customer_id,asset_cash_deposit,asset_share,asset_motor_vehicle,asset_furniture,asset_other,asset_superannuation)
								 VALUES ('$customer_id','$cashdeposits','$shares','$motorvehicles','$furniture','$other_assets','$superannuation_lifeinsurance')";
	
	$sql_new_customer_small_assets_result = mysqli_query($connect, $sql_new_customer_small_assets);
	
	if(mysqli_affected_rows($connect) <= 0)
	{
	 	//echo " fail assets";
	 	
	}
	else
	{
		//echo " success assets";
	}
	
	if($num_of_other_property_loan == 0){
	$property_type = "homeloan";
	$sql_new_customer_liabilities_property  = "INSERT INTO `property_liability`(customer_id,ppt_liability_type,ppt_liability_lender,ppt_liability_limit,ppt_liability_interest_rate,ppt_liability_amount, ppt_liability_tbpo)
	                                 VALUES ('$customer_id','$property_type','$home_loan_lender_name','$home_loan_limit','$home_loan_current_interest_rate','$home_loan_amount_owing','$home_loan_to_be_paid_out')";
	
	$sql_new_customer_liabilities_property_result = mysqli_query($connect, $sql_new_customer_liabilities_property);
    }
    
    else if($num_of_other_property_loan == 1){
    $property_type = "homeloan";
    $property_type2 = "otherpropertloan1";    
    $sql_new_customer_liabilities_property  = "INSERT INTO `property_liability`(customer_id,ppt_liability_type,ppt_liability_lender,ppt_liability_limit,ppt_liability_interest_rate,ppt_liability_amount, ppt_liability_tbpo)
                                     VALUES ('$customer_id','$property_type','$home_loan_lender_name','$home_loan_limit','$home_loan_current_interest_rate','$home_loan_amount_owing','$home_loan_to_be_paid_out')";
    
     $sql_new_customer_liabilities_property2  = "INSERT INTO `property_liability`(customer_id,ppt_liability_type,ppt_liability_lender,ppt_liability_limit,ppt_liability_interest_rate,ppt_liability_amount, ppt_liability_tbpo)
                                     VALUES ('$customer_id','$property_type2','$other_property1_loan_lender','$other_property1_loan_limit','$other_property1_loan_current_interest_rate','$other_property1_amount_owing','$other_property1_to_be_paid_out')";
    
    $sql_new_customer_liabilities_property_result = mysqli_query($connect, $sql_new_customer_liabilities_property);
    $sql_new_customer_liabilities_property_result2 = mysqli_query($connect, $sql_new_customer_liabilities_property2);
    }
    else if($num_of_other_property_loan == 2){
    $property_type = "homeloan";
    $property_type2 = "otherpropertloan1";
    $property_type3 = "otherpropertloan2";    
  
    $sql_new_customer_liabilities_property  = "INSERT INTO `property_liability`(customer_id,ppt_liability_type,ppt_liability_lender,ppt_liability_limit,ppt_liability_interest_rate,ppt_liability_amount, ppt_liability_tbpo)
                                     VALUES ('$customer_id','$property_type','$home_loan_lender_name','$home_loan_limit','$home_loan_current_interest_rate','$home_loan_amount_owing','$home_loan_to_be_paid_out')";
    
     $sql_new_customer_liabilities_property2  = "INSERT INTO `property_liability`(customer_id,ppt_liability_type,ppt_liability_lender,ppt_liability_limit,ppt_liability_interest_rate,ppt_liability_amount, ppt_liability_tbpo)
                                     VALUES ('$customer_id','$property_type2','$other_property1_loan_lender','$other_property1_loan_limit','$other_property1_loan_current_interest_rate','$other_property1_amount_owing','$other_property1_to_be_paid_out')";
    
    $sql_new_customer_liabilities_property3  = "INSERT INTO `property_liability`(customer_id,ppt_liability_type,ppt_liability_lender,ppt_liability_limit,ppt_liability_interest_rate,ppt_liability_amount, ppt_liability_tbpo)
                                     VALUES ('$customer_id','$property_type3','$other_property2_loan_lender','$other_property2_loan_limit','$other_property2_loan_current_interest_rate','$other_property2_amount_owing','$other_property2_to_be_paid_out')";
                                     
  
    
    $sql_new_customer_liabilities_property_result = mysqli_query($connect, $sql_new_customer_liabilities_property);
    $sql_new_customer_liabilities_property_result2 = mysqli_query($connect, $sql_new_customer_liabilities_property2);
    $sql_new_customer_liabilities_property_result3 = mysqli_query($connect, $sql_new_customer_liabilities_property3);
    
    }
    else if($num_of_other_property_loan == 3){
    $property_type = "homeloan";
    $property_type2 = "otherpropertloan1";
    $property_type3 = "otherpropertloan2";   
    $property_type4 = "otherpropertloan3";     
      
    $sql_new_customer_liabilities_property  = "INSERT INTO `property_liability`(customer_id,ppt_liability_type,ppt_liability_lender,ppt_liability_limit,ppt_liability_interest_rate,ppt_liability_amount, ppt_liability_tbpo)
                                     VALUES ('$customer_id','$property_type','$home_loan_lender_name','$home_loan_limit','$home_loan_current_interest_rate','$home_loan_amount_owing','$home_loan_to_be_paid_out')";
    
     $sql_new_customer_liabilities_property2  = "INSERT INTO `property_liability`(customer_id,ppt_liability_type,ppt_liability_lender,ppt_liability_limit,ppt_liability_interest_rate,ppt_liability_amount, ppt_liability_tbpo)
                                     VALUES ('$customer_id','$property_type2','$other_property1_loan_lender','$other_property1_loan_limit','$other_property1_loan_current_interest_rate','$other_property1_amount_owing','$other_property1_to_be_paid_out')";
    
    $sql_new_customer_liabilities_property3  = "INSERT INTO `property_liability`(customer_id,ppt_liability_type,ppt_liability_lender,ppt_liability_limit,ppt_liability_interest_rate,ppt_liability_amount, ppt_liability_tbpo)
                                     VALUES ('$customer_id','$property_type3','$other_property2_loan_lender','$other_property2_loan_limit','$other_property2_loan_current_interest_rate','$other_property2_amount_owing','$other_property2_to_be_paid_out')";
    
    $sql_new_customer_liabilities_property4  = "INSERT INTO `property_liability`(customer_id,ppt_liability_type,ppt_liability_lender,ppt_liability_limit,ppt_liability_interest_rate,ppt_liability_amount, ppt_liability_tbpo)
                                     VALUES ('$customer_id','$property_type4','$other_property3_loan_lender','$other_property3_loan_limit','$other_property3_loan_current_interest_rate','$other_property3_amount_owing','$other_property3_to_be_paid_out')";                                 
    
    $sql_new_customer_liabilities_property_result = mysqli_query($connect, $sql_new_customer_liabilities_property);
    $sql_new_customer_liabilities_property_result2 = mysqli_query($connect, $sql_new_customer_liabilities_property2);
    $sql_new_customer_liabilities_property_result3 = mysqli_query($connect, $sql_new_customer_liabilities_property3);
    $sql_new_customer_liabilities_property_result4 = mysqli_query($connect, $sql_new_customer_liabilities_property4);
    }
    else if($num_of_other_property_loan == 4){
    $property_type = "homeloan";
    $property_type2 = "otherpropertloan1";
    $property_type3 = "otherpropertloan2";   
    $property_type4 = "otherpropertloan3";     
    $property_type5 = "otherpropertloan4";    
    $sql_new_customer_liabilities_property  = "INSERT INTO `property_liability`(customer_id,ppt_liability_type,ppt_liability_lender,ppt_liability_limit,ppt_liability_interest_rate,ppt_liability_amount, ppt_liability_tbpo)
                                     VALUES ('$customer_id','$property_type','$home_loan_lender_name','$home_loan_limit','$home_loan_current_interest_rate','$home_loan_amount_owing','$home_loan_to_be_paid_out')";
    
    $sql_new_customer_liabilities_property2  = "INSERT INTO `property_liability`(customer_id,ppt_liability_type,ppt_liability_lender,ppt_liability_limit,ppt_liability_interest_rate,ppt_liability_amount, ppt_liability_tbpo)
                                     VALUES ('$customer_id','$property_type2','$other_property1_loan_lender','$other_property1_loan_limit','$other_property1_loan_current_interest_rate','$other_property1_amount_owing','$other_property1_to_be_paid_out')";
    
    $sql_new_customer_liabilities_property3  = "INSERT INTO `property_liability`(customer_id,ppt_liability_type,ppt_liability_lender,ppt_liability_limit,ppt_liability_interest_rate,ppt_liability_amount, ppt_liability_tbpo)
                                     VALUES ('$customer_id','$property_type3','$other_property2_loan_lender','$other_property2_loan_limit','$other_property2_loan_current_interest_rate','$other_property2_amount_owing','$other_property2_to_be_paid_out')";
    
    $sql_new_customer_liabilities_property4  = "INSERT INTO `property_liability`(customer_id,ppt_liability_type,ppt_liability_lender,ppt_liability_limit,ppt_liability_interest_rate,ppt_liability_amount, ppt_liability_tbpo)
                                     VALUES ('$customer_id','$property_type4','$other_property3_loan_lender','$other_property3_loan_limit','$other_property3_loan_current_interest_rate','$other_property3_amount_owing','$other_property3_to_be_paid_out')";
    
    $sql_new_customer_liabilities_property5  = "INSERT INTO `property_liability`(customer_id,ppt_liability_type,ppt_liability_lender,ppt_liability_limit,ppt_liability_interest_rate,ppt_liability_amount, ppt_liability_tbpo)
                                     VALUES ('$customer_id','$property_type5','$other_property4_loan_lender','$other_property4_loan_limit','$other_property4_loan_current_interest_rate','$other_property4_amount_owing','$other_property4_to_be_paid_out')";                                                                                             
    
    $sql_new_customer_liabilities_property_result = mysqli_query($connect, $sql_new_customer_liabilities_property);
    $sql_new_customer_liabilities_property_result2 = mysqli_query($connect, $sql_new_customer_liabilities_property2);
    $sql_new_customer_liabilities_property_result3 = mysqli_query($connect, $sql_new_customer_liabilities_property3);
    $sql_new_customer_liabilities_property_result4 = mysqli_query($connect, $sql_new_customer_liabilities_property4);
    $sql_new_customer_liabilities_property_result5 = mysqli_query($connect, $sql_new_customer_liabilities_property5);
    }
    if(mysqli_affected_rows($connect) <= 0)
    {
        //echo " fail liabilities property";
    }
    else
    {
        //echo " success liabilities property";
        //echo"$num_of_other_property_loan";
    }
    
    
    $sql_new_customer_liabilities_credit_card = "INSERT INTO `small_liability`(customer_id,liability_name,liability_total_limit,liability_amount,liability_tbpo)
                                     VALUES ('$customer_id','$credit_card_title_label','$credit_card_total_limit','$credit_card_amount_owing','$credit_card_to_be_paid_out')";                           
    
    $sql_new_customer_liabilities_store_card="INSERT INTO `small_liability`(customer_id,liability_name,liability_total_limit,liability_amount,liability_tbpo)
                                                VALUES ('$customer_id','$store_card_title_label','$store_card_total_limit','$store_card_amount_owing','$store_card_to_be_paid_out')";
                                                
    $sql_new_customer_liabilities_margin_loan="INSERT INTO `small_liability`(customer_id,liability_name,liability_amount,liability_tbpo)
                                                VALUES ('$customer_id','$margin_loan_title_label','$margin_loans_amount_owing','$margin_loans_to_be_paid_out')";  
                                                
    $sql_new_customer_liabilities_vehicle_finance="INSERT INTO `small_liability`(customer_id,liability_name,liability_amount,liability_tbpo)
                                                VALUES ('$customer_id','$vehicle_finanace_title_label','$vehicle_finance_amount_owing','$vehicle_finance_to_be_paid_out')";                                                                                      
    
    $sql_new_customer_liabilities_contingent_liabilities="INSERT INTO `small_liability`(customer_id,liability_name,liability_amount,liability_tbpo)
                                                VALUES ('$customer_id','$contingent_liabilities_title_label','$contingent_liabilities_amount_owing','$contingent_liabilities_to_be_paid_out')";
    
    $sql_new_customer_liabilities_personal_loans="INSERT INTO `small_liability`(customer_id,liability_name,liability_amount,liability_tbpo)
                                                VALUES ('$customer_id','$personal_loans_label','$personal_loans_amount_owing','$personal_loans_to_be_paid_out')";
    
    $sql_new_customer_liabilities_credit_card_result = mysqli_query($connect, $sql_new_customer_liabilities_credit_card);
    $sql_new_customer_liabilities_store_card_result = mysqli_query($connect, $sql_new_customer_liabilities_store_card);
    $sql_new_customer_liabilities_margin_loan_result = mysqli_query($connect, $sql_new_customer_liabilities_margin_loan);
    $sql_new_customer_liabilities_vehicle_finance_result = mysqli_query($connect, $sql_new_customer_liabilities_vehicle_finance);
    $sql_new_customer_liabilities_contingent_liabilities_result = mysqli_query($connect, $sql_new_customer_liabilities_contingent_liabilities);
    $sql_new_customer_liabilities_personal_loans_result = mysqli_query($connect, $sql_new_customer_liabilities_personal_loans);
     if(mysqli_affected_rows($connect) <= 0)
    {
        //echo " fail credit card liabilities";
    }
    else
    {
        //echo " success credit card liabilities";
        //echo "test ";
        
    }
    
    $sql_new_customer_monthly_gross_income="INSERT INTO `gross_income`(customer_id
                                                        ,salary
                                                        ,overtime
                                                        ,commission
                                                        ,rental
                                                        ,dividend
                                                        ,family_allowance
                                                        ,motor_vehicle_allowance
                                                        ,foreign_income
                                                        ,foreign_income_country
                                                        ,income_other)
                                                        VALUES ('$customer_id'
                                                        ,'$gross_salary_applicant_1'
                                                        ,'$overtime_applicant_1'
                                                        ,'$comission_bonus_applicant_1'
                                                        ,'$rental_income_applicant_1'
                                                        ,'$dividend_interest_income_applicant_1'
                                                        ,'$family_allowance_income_applicant_1'
                                                        ,'$motor_vehicle_allowance_applicant_1'
                                                        ,'$foreign_income_specify_applicant_1'
                                                        ,'$foreign_income_applicant_1'
                                                        ,'$other_income_applicant_1')";

    $sql_new_customer_monthly_gross_income_result = mysqli_query($connect, $sql_new_customer_monthly_gross_income);
    
    if(mysqli_affected_rows($connect) <= 0)
    {
        //echo " fail monthly gross income";
    }
    else
    {
        //echo " success monthly gross income";
        
    }
    
    $sql_new_customer_monthly_gross_expenses="INSERT INTO `expenses`(customer_id,living_expenses,home_loan,other_loan,credit_card,store_card,vehicle_finance,personal_loan,rent,expenses_other_specify,expenses_other_amount)
                                                VALUES ('$customer_id','$living_expense_applicant_1'
                                                ,'$home_loan_payment_applicant_1'
                                                ,'$other_loan_payment_applicant_1'
                                                ,'$credit_card_payment_applicant_1'
                                                ,'$store_card_payment_applicant_1'
                                                ,'$vehicle_finance_payment_applicant_1'
                                                ,'$personal_loan_payment_applicant_1'
                                                ,'$rent_board_applicant_1'
                                                ,'$other_specify_applicant_1'
                                                ,'$other_applicant_1')";

    $sql_new_customer_monthly_gross_expenses_result = mysqli_query($connect, $sql_new_customer_monthly_gross_expenses);
    
    if(mysqli_affected_rows($connect) <= 0)
    {
        //echo " fail monthly gross expenses";
    }
    else
    {
        //echo " success monthly gross expenses";
        
    }
    
    
    $sql_new_customer_security_property1="INSERT INTO `property`(customer_id
                                                ,property_title
                                                ,property_proposed_name
                                                ,property_type
                                                ,property_studio_size
                                                ,property_flatunit_size
                                                ,property_status
                                                ,property_use
                                                ,property_price
                                                ,property_settlement_date
                                                ,property_address
                                                ,property_postcode
                                                ,property_vp_name
                                                ,property_vp_type
                                                ,property_vp_phone_ext
                                                ,property_vp_phone_number
                                                ,property_vp_fax_ext
                                                ,property_vp_fax_number
                                                ,property_vp_mobile_number)
                                                VALUES ('$customer_id'
                                                ,'$title_particulars1'
                                                ,'$title_name1'
                                                ,'$type_of_property1'
                                                ,'$if_studio1'
                                                ,'$if_flat_unit1'
                                                ,'$property_status1'
                                                ,'$property_use1'
                                                ,'$purchased_price_estimated_value1'
                                                ,'$settlement_date1'
                                                ,'$address_security_property1'
                                                ,'$address_postalcode_security_property1'
                                                ,'$name_contact_person_valuation1'
                                                ,'$contact_person_valuation1'
                                                ,'$contact_person_phone_number1_ext'
                                                ,'$contact_person_phone_number1'
                                                ,'$contact_person_fax_number1_ext'
                                                ,'$contact_person_fax_number1'
                                                ,'$contact_person_mobile_number1')";
                                                
    $sql_new_customer_security_property2="INSERT INTO `property`(customer_id
                                                ,property_title
                                                ,property_proposed_name
                                                ,property_type
                                                ,property_studio_size
                                                ,property_flatunit_size
                                                ,property_status
                                                ,property_use
                                                ,property_price
                                                ,property_settlement_date
                                                ,property_address
                                                ,property_postcode
                                                ,property_vp_name
                                                ,property_vp_type
                                                ,property_vp_phone_ext
                                                ,property_vp_phone_number
                                                ,property_vp_fax_ext
                                                ,property_vp_fax_number
                                                ,property_vp_mobile_number)
                                                VALUES ('$customer_id'
                                                ,'$title_particulars2'
                                                ,'$title_name2'
                                                ,'$type_of_property2'
                                                ,'$if_studio2'
                                                ,'$if_flat_unit2'
                                                ,'$property_status2'
                                                ,'$property_use2'
                                                ,'$purchased_price_estimated_value2'
                                                ,'$settlement_date2'
                                                ,'$address_security_property2'
                                                ,'$address_postalcode_security_property2'
                                                ,'$name_contact_person_valuation2'
                                                ,'$contact_person_valuation2'
                                                ,'$contact_person_phone_number2_ext'
                                                ,'$contact_person_phone_number2'
                                                ,'$contact_person_fax_number2_ext'
                                                ,'$contact_person_fax_number2'
                                                ,'$contact_person_mobile_number2')";   
    
      $sql_new_customer_deposit_under_lien="INSERT INTO `deposit_under_lien`(customer_id
                                                ,deposit_under_lien_taken
                                                ,deposit_under_lien_amt
                                                ,deposit_under_lien_acc)
                                                VALUES ('$customer_id'
                                                ,'$deposit_under_lien'
                                                ,'$deposit_under_lien_amount'
                                                ,'$deposit_under_lien_acc')";                                           

    $sql_new_customer_security_propert1_result = mysqli_query($connect, $sql_new_customer_security_property1);
    $sql_new_customer_security_propert2_result = mysqli_query($connect, $sql_new_customer_security_property2);
     $sql_new_customer_deposit_under_lien_result = mysqli_query($connect, $sql_new_customer_deposit_under_lien);
    
    
    if(mysqli_affected_rows($connect) <= 0)
    {
        //echo " fail security property 1";
    }
    else
    {
        //echo " success security property 2";
        
    }
    
      $sql_new_customer_solicitor_conveyancer="INSERT INTO `solicitor`(customer_id
                                                ,solicitor_film_name
                                                ,solicitor_name
                                                ,solicitor_address
                                                ,solicitor_postcode
                                                ,solicitor_phone_ext
                                                ,solicitor_phone_number
                                                ,solicitor_fax_ext
                                                ,solicitor_fax_number
                                                ,solicitor_email    
                                                ,solicitor_document_post_to)
                                                VALUES ('$customer_id'
                                                ,'$name_of_firm'
                                                ,'$name_of_contact_person_at_firm'
                                                ,'$address_solicitor_conveyancer'
                                                ,'$address_postalcode_solicitor_conveyancer'
                                                ,'$solicitor_conveyancer_phone_number_ext'
                                                ,'$solicitor_conveyancer_phone_number'
                                                ,'$solicitor_conveyancer_fax_number_ext'
                                                ,'$solicitor_conveyancer_fax_number'
                                                ,'$solicitor_conveyancer_email'
                                                ,'$solicitor_conveyancer_document_sent')";  
                                                
        $sql_new_customer_solicitor_conveyancer_result = mysqli_query($connect, $sql_new_customer_solicitor_conveyancer);
    
    
    if(mysqli_affected_rows($connect) <= 0)
    {
        //echo " fail solicitor conveyancer";
    }
    else
    {
        //echo " success solicitor conveyancer";
        
    }    
    
    $sql_new_customer_loan_fee="INSERT INTO `loan_fee`(customer_id
                                                ,no_property_value
                                                ,establisment_fee
                                                ,fixed_rate_lock_fee
                                                ,valuation_fee
                                                ,fee_payment_type
                                                ,fee_payment_HSBC_bank_acc)
                                                VALUES ('$customer_id'
                                                ,'$no_of_properties_to_be_value'
                                                ,'$establishment_fee'
                                                ,'$fixed_rate_lock_fee'
                                                ,'$valuation_fee'
                                                ,'$fee_payment_type'
                                                ,'$HSBC_account')";  
                                                
        $sql_new_customer_loan_fee_result = mysqli_query($connect, $sql_new_customer_loan_fee);
    
    
    if(mysqli_affected_rows($connect) <= 0)
    {
        //echo " fail loan fee";
    }
    else
    {
        //echo " success loan fee";
        
    } 
    
    
    $sql_new_customer_quotation_type="INSERT INTO `building_and_contents_insurance`(customer_id
                                                ,insurance_quotation
                                                ,quotation_type
                                                ,existing_insurance_company)
                                                VALUES ('$customer_id'
                                                ,'$insurance_quotation'
                                                ,'$insurance_quotation_yes'
                                                ,'$existing_insurance')";  
                                                
        $sql_new_customer_quotation_type= mysqli_query($connect, $sql_new_customer_quotation_type);
    
    
    if(mysqli_affected_rows($connect) <= 0)
    {
        //echo " fail quotation type";
    }
    else
    {
        //echo " success quotation type";
        
    } 
    
     
    $sql_new_customer_insurance_mortgage_protection="INSERT INTO `life_insurance_mortgage_protection`(customer_id
                                                ,assist_life_mortgage_insurance
                                                ,assist_life_mortage_insurance_type)
                                                VALUES ('$customer_id'
                                                ,'$assist_life_mortgage_insurance'
                                                ,'$assist_life_mortage_insurance_type')";  
                                                
       
     $sql_new_customer_insurance_mortgage_protection_result = mysqli_query($connect, $sql_new_customer_insurance_mortgage_protection);
     
    // checks if var1 and var 2 is set
     
     
    if(mysqli_affected_rows($connect) <= 0)
    {
        //echo " fail insurance mortgage protection";
    }
    else
    {
        //echo " success insurance mortgage protection";
        
    } 

	echo "Credit Score: $creditScore ";

	if($creditScore > 14)
	{
	    
		$_SESSION['application_result'] = "accepted";
		echo " loan accepted $get_loan_no_result $current_date";
	}
	elseif($creditScore < 10)
	{
		$_SESSION['application_result'] = "rejected";
		echo " loan rejected $get_loan_no_result $current_date";
	}
	elseif(($creditScore >11) && ($creditScore < 14))
	{
		$_SESSION['application_result'] = "review";
		echo " loan pending $get_loan_no_result $current_date" ;
	}

}

//echo $surname;

?>